package o;

import android.content.Context;
import android.content.res.Resources;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import androidx.appcompat.view.menu.MenuAdapter;
import androidx.core.view.ViewCompat;
import o.ah.e;
import o.bn.d;

final class bw extends bm implements OnDismissListener, OnItemClickListener, OnKeyListener {
    private static final int ˋ = ah$j.ॱˊ;
    private final int ʻ;
    private int ʻॱ;
    private final boolean ʼ;
    private final Context ʽ;
    private boolean ʿ;
    final cn ˊ;
    private final OnAttachStateChangeListener ˊॱ = new OnAttachStateChangeListener(this) {
        final /* synthetic */ bw ˎ;

        {
            this.ˎ = r1;
        }

        public void onViewAttachedToWindow(View view) {
        }

        public void onViewDetachedFromWindow(View view) {
            if (this.ˎ.ॱ != null) {
                if (!this.ˎ.ॱ.isAlive()) {
                    this.ˎ.ॱ = view.getViewTreeObserver();
                }
                this.ˎ.ॱ.removeGlobalOnLayoutListener(this.ˎ.ˎ);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    };
    private View ˋॱ;
    final OnGlobalLayoutListener ˎ = new OnGlobalLayoutListener(this) {
        final /* synthetic */ bw ॱ;

        {
            this.ॱ = r1;
        }

        public void onGlobalLayout() {
            if (this.ॱ.ˎ() && !this.ॱ.ˊ.ॱॱ()) {
                View view = this.ॱ.ˏ;
                if (view == null || !view.isShown()) {
                    this.ॱ.ˋ();
                } else {
                    this.ॱ.ˊ.ˊ();
                }
            }
        }
    };
    View ˏ;
    private final int ˏॱ;
    private OnDismissListener ͺ;
    ViewTreeObserver ॱ;
    private final int ॱˊ;
    private boolean ॱˋ;
    private int ॱˎ = 0;
    private final MenuAdapter ॱॱ;
    private d ॱᐝ;
    private final bd ᐝ;
    private boolean ᐝॱ;

    public bw(Context context, bd bdVar, View view, int i, int i2, boolean z) {
        this.ʽ = context;
        this.ᐝ = bdVar;
        this.ʼ = z;
        this.ॱॱ = new MenuAdapter(bdVar, LayoutInflater.from(context), this.ʼ, ˋ);
        this.ˏॱ = i;
        this.ॱˊ = i2;
        Resources resources = context.getResources();
        this.ʻ = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(e.ˋ));
        this.ˋॱ = view;
        this.ˊ = new cn(this.ʽ, null, this.ˏॱ, this.ॱˊ);
        bdVar.ˏ(this, context);
    }

    public void ˎ(boolean z) {
        this.ॱॱ.setForceShowIcon(z);
    }

    public void ॱ(int i) {
        this.ॱˎ = i;
    }

    private boolean ʼ() {
        if (ˎ()) {
            return true;
        }
        if (this.ॱˋ || this.ˋॱ == null) {
            return false;
        }
        this.ˏ = this.ˋॱ;
        this.ˊ.ॱ((OnDismissListener) this);
        this.ˊ.ˊ((OnItemClickListener) this);
        this.ˊ.ˎ(true);
        View view = this.ˏ;
        boolean z = this.ॱ == null;
        this.ॱ = view.getViewTreeObserver();
        if (z) {
            this.ॱ.addOnGlobalLayoutListener(this.ˎ);
        }
        view.addOnAttachStateChangeListener(this.ˊॱ);
        this.ˊ.ˏ(view);
        this.ˊ.ॱ(this.ॱˎ);
        if (!this.ᐝॱ) {
            this.ʻॱ = bm.ˏ(this.ॱॱ, null, this.ʽ, this.ʻ);
            this.ᐝॱ = true;
        }
        this.ˊ.ʼ(this.ʻॱ);
        this.ˊ.ʻ(2);
        this.ˊ.ˏ(ॱॱ());
        this.ˊ.ˊ();
        ViewGroup ᐝ = this.ˊ.ᐝ();
        ᐝ.setOnKeyListener(this);
        if (this.ʿ && this.ᐝ.ˏॱ() != null) {
            FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.ʽ).inflate(ah$j.ˏॱ, ᐝ, false);
            TextView textView = (TextView) frameLayout.findViewById(16908310);
            if (textView != null) {
                textView.setText(this.ᐝ.ˏॱ());
            }
            frameLayout.setEnabled(false);
            ᐝ.addHeaderView(frameLayout, null, false);
        }
        this.ˊ.ˏ(this.ॱॱ);
        this.ˊ.ˊ();
        return true;
    }

    public void ˊ() {
        if (!ʼ()) {
            throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor");
        }
    }

    public void ˋ() {
        if (ˎ()) {
            this.ˊ.ˋ();
        }
    }

    public void ˊ(bd bdVar) {
    }

    public boolean ˎ() {
        return !this.ॱˋ && this.ˊ.ˎ();
    }

    public void onDismiss() {
        this.ॱˋ = true;
        this.ᐝ.close();
        if (this.ॱ != null) {
            if (!this.ॱ.isAlive()) {
                this.ॱ = this.ˏ.getViewTreeObserver();
            }
            this.ॱ.removeGlobalOnLayoutListener(this.ˎ);
            this.ॱ = null;
        }
        this.ˏ.removeOnAttachStateChangeListener(this.ˊॱ);
        if (this.ͺ != null) {
            this.ͺ.onDismiss();
        }
    }

    public void ˊ(boolean z) {
        this.ᐝॱ = false;
        if (this.ॱॱ != null) {
            this.ॱॱ.notifyDataSetChanged();
        }
    }

    public void ˎ(d dVar) {
        this.ॱᐝ = dVar;
    }

    public boolean ˎ(bv bvVar) {
        if (bvVar.hasVisibleItems()) {
            br brVar = new br(this.ʽ, bvVar, this.ˏ, this.ʼ, this.ˏॱ, this.ॱˊ);
            brVar.ˋ(this.ॱᐝ);
            brVar.ˏ(bm.ˏ((bd) bvVar));
            brVar.ˊ(this.ͺ);
            this.ͺ = null;
            this.ᐝ.ˏ(false);
            int ʽ = this.ˊ.ʽ();
            int ˏॱ = this.ˊ.ˏॱ();
            if ((Gravity.getAbsoluteGravity(this.ॱˎ, ViewCompat.ʼ(this.ˋॱ)) & 7) == 5) {
                ʽ += this.ˋॱ.getWidth();
            }
            if (brVar.ˋ(ʽ, ˏॱ)) {
                if (this.ॱᐝ != null) {
                    this.ॱᐝ.ˎ(bvVar);
                }
                return true;
            }
        }
        return false;
    }

    public void ˏ(bd bdVar, boolean z) {
        if (bdVar == this.ᐝ) {
            ˋ();
            if (this.ॱᐝ != null) {
                this.ॱᐝ.ॱ(bdVar, z);
            }
        }
    }

    public boolean ˏ() {
        return false;
    }

    public Parcelable ʽ() {
        return null;
    }

    public void ˏ(Parcelable parcelable) {
    }

    public void ˎ(View view) {
        this.ˋॱ = view;
    }

    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() != 1 || i != 82) {
            return false;
        }
        ˋ();
        return true;
    }

    public void ॱ(OnDismissListener onDismissListener) {
        this.ͺ = onDismissListener;
    }

    public ListView ᐝ() {
        return this.ˊ.ᐝ();
    }

    public void ˏ(int i) {
        this.ˊ.ˎ(i);
    }

    public void ˎ(int i) {
        this.ˊ.ˏ(i);
    }

    public void ॱ(boolean z) {
        this.ʿ = z;
    }
}
